<template>
	<view>
		<text class="text-lg">个人中心</text>
		
		<radio-group style="border-top: 1upx solid #eee;" name="ischoose" class="block" @change="updatebaby">
			<view class="cu-form-group" v-if="babys!=''">
				<view class="title">我的宝：</view>
				<label v-for="item,index in babys" :key="index">
					<radio :value="String(item.id)" :checked="item.ischoose?true:false" />
					<text>{{item.babynike}}</text>
				</label>
			</view>
			<view v-else>添加数据</view>
		</radio-group>
		
		<view class="cu-form-group">
			<view class="title">宝宝性别：</view>
		</view>

		<view class="cu-form-group margin-top">
			<view class="title">宝宝小名：</view>
			<input type="text" v-model="babyconent.babynike" placeholder="输入宝宝名称" />
		</view>
		<radio-group style="border-top: 1upx solid #eee;" name="bb_id" class="block" @change="RadioChange">
			<view class="cu-form-group">
				<view class="title">宝宝性别：</view>
				<label>
					<radio value="0" /><text>女宝</text>
				</label>
				<label>
					<radio value="1" /><text>男宝</text>
				</label>
				<label>
					<radio value="2" checked="true" /><text>保密</text>
				</label>
			</view>
		</radio-group>

		<view class="cu-form-group" style="border-top: 1upx solid #eee;">
			<view class="title">宝宝生日：</view>
			<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="DateChange">
				<view class="picker">
					{{date}}
				</view>
			</picker>
		</view>
		<input type="number" :value="babyconent.user_id" name='user_id' v-show="false" />
		<view class="cu-form-group">
			<view class="title">状 态：</view>
			<switch @change="SwitchA" :class="switchA?'checked':''" :checked="switchA?true:false"></switch>
			</switch>
		</view>

		<view class="padding flex flex-direction margin-top">
			<button class="cu-btn bg-red margin-tb-sm lg" @click="addbaby">确定</button>
			<button class="cu-btn margin-tb-sm bg-gradual-green lg" @click="back">返回</button>
		</view>
		{{babyconent}}
	</view>
</template>

<script>
	import url_config from '@/common/config.js'
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				id: 0,
				babyconent: {
					user_id: 1,
					babynike: '',
					sex: 0,
					birthday: '2022-02-03',
					status: 0
				},
				babys: [],
				switchA: false,
				date: currentDate,
			}
		},
		methods: {
			SwitchA(e) {
				//把布尔值转化成0/1.。。。呵呵哒
				this.switchA = e.detail.value
				if (e.detail.value) {
					this.babyconent.status = 1
				} else {
					this.babyconent.status = 0
				}

			},
			updatebaby(e){
				//点击设置当前展示的宝宝
				console.log(e.detail.value)
				//修改默认展示，ischoose=1/0
				//后端增加一个修改模块
			},
			RadioChange(e) {
				this.babyconent.sex = e.detail.value
			},
			DateChange(e) {
				this.date = e.detail.value
				this.babyconent.birthday = e.detail.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			addbaby() {
				//添加数据
				uni.request({
					url: url_config.api + 'addBaby',
					data: this.babyconent,
					method: 'POST',
					success: (res) => {
						console.log(res.data)
						try{
							uni.setStorageSync('bb_id',res.data.data)
							console.log('缓存ID:' + res.data.data)
						}catch(e){
							//TODO handle the exception
						}
						uni.showToast({
							title: '添加成功',
							duration: 2000
						})
					},
					fail: () => {
						uni.showToast({
							title: '添加失败',
							duration: 2000
						})
					}
				})
			},
			getBaby(uid) {
				//获取当前登录用户添加到宝宝
				console.log('获取当前登录用户添加到宝宝' + uid)
				uni.request({
					url: url_config.api + 'getBaby/' + uid,
					success: (res) => {
						this.babys = res.data.data
					}
				})
			},
			back() {
				//关闭当前页面后，跳转到指定页
				uni.redirectTo({
					url: '../baby/baby'
				})
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}

		},
		onLoad: function(option) {
			//获取上一个页面传递过来的参数
			this.babyconent.user_id = option.user_id;
			console.log(option)
			//获取宝宝数据
			this.getBaby(option.user_id)
		}
	}
</script>

<style>

</style>
